<?php

namespace app\common\customer\logic;

use app\common\BaseLogic;
use app\common\customer\model\CustomerAllocate;
use app\common\customer\model\CustomerInvite;

class CheckinLogic extends BaseLogic
{

    /**
     * 进店客资
     * 首次 =  非异业渠道
     * 转介绍 = 异业渠道[6]
     * @param $userIds
     * @param $dateRange
     * @return array
     */
    public function statistics($userIds, $dateRange)
    {
        $sourceLogic = new \app\common\config\logic\SourceLogic();
        $diffSourceIds = $sourceLogic->getCooperation();

        // 进店总数
        $model = new CustomerAllocate();
        $where = [];
        $where[] = ['user_id', 'in', $userIds];
        $where[] = ['status', '=', 24];
        $where[] = ['create_time', 'between', $dateRange];
        $total = $model->where($where)->count();

        $total2 = $model->where($where)->whereIn('member_id', function ($query) use ($diffSourceIds) {
            $query->table('tk_member')->whereIn('source_id', $diffSourceIds)->field('member_id');
        })->count();

        $total1 = $total - $total2;

        return [
            [
                'title' => '首次',
                'total' => $total1,
            ],
            [
                'title' => '转介绍',
                'total' => $total2,
            ],
            [
                'title' => '合计',
                'total' => $total,
            ],
        ];
    }

}